﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ARR.Model.Repositories;
using ARR.Entities.Gateways;
using ARR.DataModel.Repositories;
using ARR.Entities;
using System.Transactions;
using DDF.Entity;

namespace ARR.Services.Domain
{
    public static class HistoryDomain
    {
        public static History GetById(int historyId)
        {
            var row = HistoryRepository.Select(x => x.Id == historyId).SingleOrDefault();
            var history = new History(row);

            history.ChangeState(EntityState.Original);

            return history;
        }

        public static void GenerateHistory(int rangeReviewId)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                HistoryDetailsRepository.ClearOldHistory(rangeReviewId);
                HistoryDetailsRepository.GenerateHistory(rangeReviewId);

                //Agrego un registro para TOTAL STORE.
                List<HistoryDetail> details = GetCurrentRangeReviewHistory(rangeReviewId);
                if (details != null && details.Any())
                {
                    var query = from d in details
                                group d by new { d.Range, d.HistoryId, d.Country } into g
                                select new HistoryDetailRow
                                {
                                    Country = g.Key.Country,
                                    Range = g.Key.Range,
                                    HistoryId = g.Key.HistoryId,
                                    Category = "TOTAL STORE",
                                    AverageVolume = g.Sum(x => Convert.ToDecimal(x.AverageVolume)),
                                    AverageSales = g.Sum(x => x.AverageSales),
                                    AverageGIM = g.Sum(x => x.AverageGIM),
                                    AverageDelistedVolume = g.Sum(x => Convert.ToDecimal(x.AverageDelistedVolume)),
                                    AverageDelistedSales = g.Sum(x => x.AverageDelistedSales),
                                    AverageDelistedGIM = g.Sum(x => x.AverageDelistedGIM),
                                    SS_VolumeCY = g.Sum(x => Convert.ToDecimal(x.SS_VolumeCY)),
                                    SS_SalesCY = g.Sum(x => x.SS_SalesCY),
                                    SS_GIMCY = g.Sum(x => x.SS_GIMCY),
                                    SS_PIDiffCY = g.Sum(x => x.SS_PIDiffCY),
                                    SS_ChargeOffCY = g.Sum(x => x.SS_ChargeOffCY),
                                    SS_RebatesCY = g.Sum(x => x.SS_RebatesCY),
                                    SS_InventoryCY = g.Sum(x => x.SS_InventoryCY),
                                    SS_VolumePY = g.Sum(x => Convert.ToDecimal(x.SS_VolumePY)),
                                    SS_SalesPY = g.Sum(x => x.SS_SalesPY),
                                    SS_GIMPY = g.Sum(x => x.SS_GIMPY),
                                    SS_PIDiffPY = g.Sum(x => x.SS_PIDiffPY),
                                    SS_ChargeOffPY = g.Sum(x => x.SS_ChargeOffPY),
                                    SS_RebatesPY = g.Sum(x => x.SS_RebatesPY),
                                    SS_InventoryPY = g.Sum(x => x.SS_InventoryPY),
                                    Gondola = g.Sum(x => x.Gondola),
                                    TransactionArea = g.Sum(x => x.TransactionArea),
                                    GrabAndGo = g.Sum(x => x.GrabAndGo),
                                    WIC = g.Sum(x => x.WIC),
                                    OpenCooler = g.Sum(x => x.OpenCooler),
                                    Freezer = g.Sum(x => x.Freezer),
                                    Forecourt = g.Sum(x => x.Forecourt),
                                    OtherExhibition = g.Sum(x => x.OtherExhibition),
                                    SKUsWithSales = g.Sum(x => x.SKUsWithSales),
                                    NewSKUs = g.Sum(x => x.NewSKUs),
                                    SKUsInSalesFiles = g.Sum(x => x.SKUsInSalesFiles),
                                    DelistProposal = g.Sum(x => x.DelistProposal),
                                    ActiveCategories = g.Sum(x => x.ActiveCategories),
                                    CategoriesWithInvAnalysis = g.Sum(x => x.CategoriesWithInvAnalysis)
                                };

                    HistoryDetailRow totalStoreRow = query.FirstOrDefault();

                    if (totalStoreRow != null)
                    {
                        if (totalStoreRow.AverageGIM.HasValue && totalStoreRow.AverageSales.HasValue && totalStoreRow.AverageSales != 0)
                        {
                            totalStoreRow.AverageGIMP = totalStoreRow.AverageGIM / totalStoreRow.AverageSales;
                        }

                        if (totalStoreRow.SS_SalesCY.HasValue && totalStoreRow.SKUsWithSales.HasValue && totalStoreRow.SKUsWithSales != 0)
                        {
                            totalStoreRow.HurdleRate = Convert.ToInt32(totalStoreRow.SS_SalesCY / totalStoreRow.SKUsWithSales);
                        }

                        var history = HistoryRepository.Select(x => x.RangeReviewId == rangeReviewId).FirstOrDefault();
                        totalStoreRow.HistoryId = history.Id;

                        HistoryDetailsRepository.Insert(totalStoreRow);
                    }
                }

                scope.Complete();
            }


        }

        public static List<HistoryDetail> GetCurrentRangeReviewHistory(int rangeReviewId)
        {
            //Primero traigo el pareto
            var paretoRows = ReportsRepository.GetSalesCategoryComparisonPareto(rangeReviewId, 0, true, 0, 0).Where(x => !x.IsDeletedPerformance).ToList();
            List<SalesCategoryComparisonPareto> paretoItems = paretoRows.ConvertAll<SalesCategoryComparisonPareto>(c => new SalesCategoryComparisonPareto(c));

            //TODO: Por ahora genero todo con el Pareto. Después ver bien tema tasas de cambio
            var paretoItemsPY = paretoItems.Where(x => x.IsPriorYear);
            var paretoItemsCY = paretoItems.Where(x => !x.IsPriorYear);

            //Traigo el espacio
            var spaceRows = ReportsRepository.GetSalesCategoryComparisonBySpace(rangeReviewId, 0, false, 1);
            List<SalesCategoryComparisonBySpace> spaces = spaceRows.ConvertAll<SalesCategoryComparisonBySpace>(c => new SalesCategoryComparisonBySpace(c));

            var categories = CategoriesRepository.Select();

            List<HistoryDetail> subcategoriesHistory = (from item in paretoItemsCY
                                                        group item by new { item.Subcategory, item.MC } into g
                                                        select new HistoryDetail
                                                        {
                                                            Category = g.Key.Subcategory,
                                                            CategoryId = categories.Where(x => x.Code == g.Key.MC).First().Id,
                                                            MC = g.Key.MC,
                                                            Level = 3,
                                                            AverageVolume = g.Where(x => !x.IsDeletedRangeReview).Sum(x => x.Units),
                                                            AverageSales = g.Where(x => !x.IsDeletedRangeReview).Sum(x => x.Sales),
                                                            AverageGIM = g.Where(x => !x.IsDeletedRangeReview).Sum(x => x.GM),
                                                            AverageGIMP = g.Where(x => !x.IsDeletedRangeReview).Sum(x => x.Sales) != 0 ? g.Where(x => !x.IsDeletedRangeReview).Sum(x => x.GM) / g.Where(x => !x.IsDeletedRangeReview).Sum(x => x.Sales) : 0,
                                                            AverageDelistedVolume = g.Where(x => x.IsDelisted).Sum(x => x.Units),
                                                            AverageDelistedSales = g.Where(x => x.IsDelisted).Sum(x => x.Sales),
                                                            AverageDelistedGIM = g.Where(x => x.IsDelisted).Sum(x => x.GM),
                                                            SKUsWithSales = g.Where(x => !x.IsDeletedRangeReview).Count(),
                                                            NewSKUs = g.Where(x => x.IsNew).Count(),
                                                            SKUsInSalesFiles = g.Where(x => !x.IsPriorYear).Count(),
                                                            DelistProposal = g.Where(x => !x.IsDeletedRangeReview && x.Delist).Count(),

                                                            SS_VolumePY = paretoItemsPY.Where(p => p.MC == g.Key.MC && p.IsPriorYear).Sum(p => p.Units),
                                                            SS_VolumeCY = g.Sum(x => x.Units),
                                                            SS_SalesPY = paretoItemsPY.Where(p => p.MC == g.Key.MC && p.IsPriorYear).Sum(x => x.Sales),
                                                            SS_SalesCY = g.Sum(x => x.Sales),
                                                            SS_GIMPY = paretoItemsPY.Where(p => p.MC == g.Key.MC && p.IsPriorYear).Sum(x => x.GM),
                                                            SS_GIMCY = g.Sum(x => x.GM),
                                                            SS_InventoryPY = paretoItemsPY.Where(p => p.MC == g.Key.MC && p.IsPriorYear).Sum(x => x.InventoryCost),
                                                            SS_InventoryCY = g.Sum(x => x.InventoryCost),
                                                            SS_PIDiffPY = paretoItemsPY.Where(p => p.MC == g.Key.MC && p.IsPriorYear).Sum(x => x.PIDiff),
                                                            SS_PIDiffCY = g.Sum(x => x.PIDiff),
                                                            SS_ChargeOffPY = paretoItemsPY.Where(p => p.MC == g.Key.MC && p.IsPriorYear).Sum(x => x.ChargeOffs),
                                                            SS_ChargeOffCY = g.Sum(x => x.ChargeOffs),
                                                            SS_RebatesPY = paretoItemsPY.Where(p => p.MC == g.Key.MC && p.IsPriorYear).Sum(x => x.Rebates),
                                                            SS_RebatesCY = g.Sum(x => x.Rebates),
                                                            Gondola = 0,
                                                            WIC = 0,
                                                            TransactionArea = 0,
                                                            GrabAndGo = 0,
                                                            OpenCooler = 0,
                                                            Freezer = 0,
                                                            Forecourt = 0,
                                                            OtherExhibition = 0,
                                                            ActiveCategories = 1
                                                        }).ToList();

            foreach (var subcategory in subcategoriesHistory)
            {
                decimal acumSales = 0;
                foreach (var item in paretoItemsCY.Where(x => x.MC == subcategory.MC && !x.IsDeletedRangeReview).OrderByDescending(x => x.Sales))
                {
                    acumSales += item.Sales;
                    item.AcumSales = subcategory.AverageSales.HasValue && subcategory.AverageSales.Value != 0 ? acumSales / subcategory.AverageSales.Value : 0;
                }

                foreach (var item in paretoItemsCY.Where(x => x.MC == subcategory.MC).OrderByDescending(x => x.Sales))
                {
                    //Spaces
                    foreach (var space in spaces.Where(x => x.Id == item.Id && x.LinearSpaceMts > 0))
                    {
                        switch (space.Location1)
                        {
                            case "Gondola":
                                subcategory.Gondola += space.LinearSpaceMts;
                                break;
                            case "WIC":
                                subcategory.WIC += space.LinearSpaceMts;
                                break;
                            case "Transaction Area":
                                subcategory.TransactionArea += space.LinearSpaceMts;
                                break;
                            case "Grab And Go":
                                subcategory.GrabAndGo += space.LinearSpaceMts;
                                break;
                            case "Open Cooler":
                                subcategory.OpenCooler += space.LinearSpaceMts;
                                break;
                            case "Freezer":
                                subcategory.Freezer += space.LinearSpaceMts;
                                break;
                            case "Forecourt":
                                subcategory.Forecourt += space.LinearSpaceMts;
                                break;
                            case "Other Exhibition":
                                subcategory.OtherExhibition += space.LinearSpaceMts;
                                break;
                            default:
                                subcategory.OtherExhibition += space.LinearSpaceMts;
                                break;
                        }
                    }
                }

                subcategory.Sales10 = paretoItemsCY.Where(x => x.MC == subcategory.MC && !x.IsDeletedRangeReview && x.AcumSales <= 0.1m).Count();
                subcategory.Sales20 = paretoItemsCY.Where(x => x.MC == subcategory.MC && !x.IsDeletedRangeReview && x.AcumSales <= 0.2m).Count();
                subcategory.Sales30 = paretoItemsCY.Where(x => x.MC == subcategory.MC && !x.IsDeletedRangeReview && x.AcumSales <= 0.3m).Count();
                subcategory.Sales40 = paretoItemsCY.Where(x => x.MC == subcategory.MC && !x.IsDeletedRangeReview && x.AcumSales <= 0.4m).Count();
                subcategory.Sales50 = paretoItemsCY.Where(x => x.MC == subcategory.MC && !x.IsDeletedRangeReview && x.AcumSales <= 0.5m).Count();
                subcategory.Sales60 = paretoItemsCY.Where(x => x.MC == subcategory.MC && !x.IsDeletedRangeReview && x.AcumSales <= 0.6m).Count();
                subcategory.Sales70 = paretoItemsCY.Where(x => x.MC == subcategory.MC && !x.IsDeletedRangeReview && x.AcumSales <= 0.7m).Count();
                subcategory.Sales80 = paretoItemsCY.Where(x => x.MC == subcategory.MC && !x.IsDeletedRangeReview && x.AcumSales <= 0.8m).Count();
                subcategory.Sales90 = paretoItemsCY.Where(x => x.MC == subcategory.MC && !x.IsDeletedRangeReview && x.AcumSales <= 0.9m).Count();
                subcategory.Sales100 = paretoItemsCY.Where(x => x.MC == subcategory.MC && !x.IsDeletedRangeReview).Count();
                subcategory.Bottom5 = paretoItemsCY.Where(x => x.MC == subcategory.MC && !x.IsDeletedRangeReview && x.AcumSales >= 0.95m).Count();

                decimal? hurdleRate = subcategory.Sales100.HasValue && subcategory.Sales100.Value != 0 ? subcategory.AverageSales / subcategory.Sales100 : null;
                subcategory.HurdleRate = paretoItemsCY.Where(x => x.MC == subcategory.MC && !x.IsDeletedRangeReview && x.Sales < hurdleRate).Count();
            }

            //Categories
            List<HistoryDetail> categoriesHistory = (from item in paretoItemsCY
                                                     group item by item.Category into g
                                                     select new HistoryDetail
                                                     {
                                                         Category = g.Key,
                                                         CategoryId = categories.Where(x => x.Name == g.Key).First().Id,
                                                         MC = categories.Where(x => x.Name == g.Key).First().Code,
                                                         Level = 1,
                                                         AverageVolume = g.Where(x => !x.IsDeletedRangeReview).Sum(x => x.Units),
                                                         AverageSales = g.Where(x => !x.IsDeletedRangeReview).Sum(x => x.Sales),
                                                         AverageGIM = g.Where(x => !x.IsDeletedRangeReview).Sum(x => x.GM),
                                                         AverageGIMP = g.Where(x => !x.IsDeletedRangeReview).Sum(x => x.Sales) != 0 ? g.Where(x => !x.IsDeletedRangeReview).Sum(x => x.GM) / g.Where(x => !x.IsDeletedRangeReview).Sum(x => x.Sales) : 0,
                                                         AverageDelistedVolume = g.Where(x => x.IsDelisted).Sum(x => x.Units),
                                                         AverageDelistedSales = g.Where(x => x.IsDelisted).Sum(x => x.Sales),
                                                         AverageDelistedGIM = g.Where(x => x.IsDelisted).Sum(x => x.GM),
                                                         SKUsWithSales = g.Where(x => !x.IsDeletedRangeReview).Count(),
                                                         NewSKUs = g.Where(x => x.IsNew).Count(),
                                                         SKUsInSalesFiles = g.Where(x => !x.IsPriorYear).Count(),
                                                         DelistProposal = g.Where(x => !x.IsDeletedRangeReview && x.Delist).Count(),

                                                         SS_VolumePY = paretoItemsPY.Where(p => p.Category == g.Key && p.IsPriorYear).Sum(p => p.Units),
                                                         SS_VolumeCY = g.Sum(x => x.Units),
                                                         SS_SalesPY = paretoItemsPY.Where(p => p.Category == g.Key && p.IsPriorYear).Sum(x => x.Sales),
                                                         SS_SalesCY = g.Sum(x => x.Sales),
                                                         SS_GIMPY = paretoItemsPY.Where(p => p.Category == g.Key && p.IsPriorYear).Sum(x => x.GM),
                                                         SS_GIMCY = g.Sum(x => x.GM),
                                                         SS_InventoryPY = paretoItemsPY.Where(p => p.Category == g.Key && p.IsPriorYear).Sum(x => x.InventoryCost),
                                                         SS_InventoryCY = g.Sum(x => x.InventoryCost),
                                                         SS_PIDiffPY = paretoItemsPY.Where(p => p.Category == g.Key && p.IsPriorYear).Sum(x => x.PIDiff),
                                                         SS_PIDiffCY = g.Sum(x => x.PIDiff),
                                                         SS_ChargeOffPY = paretoItemsPY.Where(p => p.Category == g.Key && p.IsPriorYear).Sum(x => x.ChargeOffs),
                                                         SS_ChargeOffCY = g.Sum(x => x.ChargeOffs),
                                                         SS_RebatesPY = paretoItemsPY.Where(p => p.Category == g.Key && p.IsPriorYear).Sum(x => x.Rebates),
                                                         SS_RebatesCY = g.Sum(x => x.Rebates),
                                                         Gondola = 0,
                                                         WIC = 0,
                                                         TransactionArea = 0,
                                                         GrabAndGo = 0,
                                                         OpenCooler = 0,
                                                         Freezer = 0,
                                                         Forecourt = 0,
                                                         OtherExhibition = 0,
                                                         ActiveCategories = 1
                                                     }).ToList();

            foreach (var category in categoriesHistory)
            {
                decimal acumSales = 0;
                foreach (var item in paretoItemsCY.Where(x => x.Category == category.Category && !x.IsDeletedRangeReview).OrderByDescending(x => x.Sales))
                {
                    acumSales += item.Sales;
                    item.AcumSales = category.AverageSales.HasValue ? acumSales / category.AverageSales.Value : 0;
                }

                foreach (var item in paretoItemsCY.Where(x => x.Category == category.Category).OrderByDescending(x => x.Sales))
                {
                    //Spaces
                    foreach (var space in spaces.Where(x => x.Id == item.Id && x.LinearSpaceMts > 0))
                    {
                        switch (space.Location1)
                        {
                            case "Gondola":
                                category.Gondola += space.LinearSpaceMts;
                                break;
                            case "WIC":
                                category.WIC += space.LinearSpaceMts;
                                break;
                            case "Transaction Area":
                                category.TransactionArea += space.LinearSpaceMts;
                                break;
                            case "Grab And Go":
                                category.GrabAndGo += space.LinearSpaceMts;
                                break;
                            case "Open Cooler":
                                category.OpenCooler += space.LinearSpaceMts;
                                break;
                            case "Freezer":
                                category.Freezer += space.LinearSpaceMts;
                                break;
                            case "Forecourt":
                                category.Forecourt += space.LinearSpaceMts;
                                break;
                            case "Other Exhibition":
                                category.OtherExhibition += space.LinearSpaceMts;
                                break;
                            default:
                                category.OtherExhibition += space.LinearSpaceMts;
                                break;
                        }
                    }
                }

                category.Sales10 = paretoItemsCY.Where(x => x.Category == category.Category && !x.IsDeletedRangeReview && x.AcumSales <= 0.1m).Count();
                category.Sales20 = paretoItemsCY.Where(x => x.Category == category.Category && !x.IsDeletedRangeReview && x.AcumSales <= 0.2m).Count();
                category.Sales30 = paretoItemsCY.Where(x => x.Category == category.Category && !x.IsDeletedRangeReview && x.AcumSales <= 0.3m).Count();
                category.Sales40 = paretoItemsCY.Where(x => x.Category == category.Category && !x.IsDeletedRangeReview && x.AcumSales <= 0.4m).Count();
                category.Sales50 = paretoItemsCY.Where(x => x.Category == category.Category && !x.IsDeletedRangeReview && x.AcumSales <= 0.5m).Count();
                category.Sales60 = paretoItemsCY.Where(x => x.Category == category.Category && !x.IsDeletedRangeReview && x.AcumSales <= 0.6m).Count();
                category.Sales70 = paretoItemsCY.Where(x => x.Category == category.Category && !x.IsDeletedRangeReview && x.AcumSales <= 0.7m).Count();
                category.Sales80 = paretoItemsCY.Where(x => x.Category == category.Category && !x.IsDeletedRangeReview && x.AcumSales <= 0.8m).Count();
                category.Sales90 = paretoItemsCY.Where(x => x.Category == category.Category && !x.IsDeletedRangeReview && x.AcumSales <= 0.9m).Count();
                category.Sales100 = paretoItemsCY.Where(x => x.Category == category.Category && !x.IsDeletedRangeReview).Count();
                category.Bottom5 = paretoItemsCY.Where(x => x.Category == category.Category && !x.IsDeletedRangeReview && x.AcumSales >= 0.95m).Count();

                decimal? hurdleRate = category.Sales100.HasValue && category.Sales100.Value != 0 ? category.AverageSales / category.Sales100 : null;
                category.HurdleRate = paretoItemsCY.Where(x => x.Category == category.Category && !x.IsDeletedRangeReview && x.Sales < hurdleRate).Count();
            }

            //TOTAL STORE
            var total = new HistoryDetail
                        {
                            MC = "1",
                            Category = "TOTAL STORE",
                            Level = 2,
                            AverageVolume = paretoItemsCY.Where(x => !x.IsDeletedRangeReview).Sum(x => x.Units),
                            AverageSales = paretoItemsCY.Where(x => !x.IsDeletedRangeReview).Sum(x => x.Sales),
                            AverageGIM = paretoItemsCY.Where(x => !x.IsDeletedRangeReview).Sum(x => x.GM),
                            AverageGIMP = paretoItemsCY.Where(x => !x.IsDeletedRangeReview).Sum(x => x.Sales) != 0 ? paretoItemsCY.Where(x => !x.IsDeletedRangeReview).Sum(x => x.GM) / paretoItemsCY.Where(x => !x.IsDeletedRangeReview).Sum(x => x.Sales) : 0,
                            AverageDelistedVolume = paretoItemsCY.Where(x => x.IsDelisted).Sum(x => x.Units),
                            AverageDelistedSales = paretoItemsCY.Where(x => x.IsDelisted).Sum(x => x.Sales),
                            AverageDelistedGIM = paretoItemsCY.Where(x => x.IsDelisted).Sum(x => x.GM),
                            SKUsWithSales = paretoItemsCY.Where(x => !x.IsDeletedRangeReview).Count(),
                            NewSKUs = paretoItemsCY.Where(x => x.IsNew).Count(),
                            SKUsInSalesFiles = paretoItemsCY.Where(x => !x.IsPriorYear).Count(),
                            DelistProposal = paretoItemsCY.Where(x => !x.IsDeletedRangeReview && x.Delist).Count(),

                            SS_VolumePY = paretoItemsPY.Sum(p => p.Units),
                            SS_VolumeCY = paretoItemsCY.Sum(x => x.Units),
                            SS_SalesPY = paretoItemsPY.Sum(x => x.Sales),
                            SS_SalesCY = paretoItemsCY.Sum(x => x.Sales),
                            SS_GIMPY = paretoItemsPY.Sum(x => x.GM),
                            SS_GIMCY = paretoItemsCY.Sum(x => x.GM),
                            SS_InventoryPY = paretoItemsPY.Sum(x => x.InventoryCost),
                            SS_InventoryCY = paretoItemsCY.Sum(x => x.InventoryCost),
                            SS_PIDiffPY = paretoItemsPY.Sum(x => x.PIDiff),
                            SS_PIDiffCY = paretoItemsCY.Sum(x => x.PIDiff),
                            SS_ChargeOffPY = paretoItemsPY.Sum(x => x.ChargeOffs),
                            SS_ChargeOffCY = paretoItemsCY.Sum(x => x.ChargeOffs),
                            SS_RebatesPY = paretoItemsPY.Sum(x => x.Rebates),
                            SS_RebatesCY = paretoItemsCY.Sum(x => x.Rebates),
                            Gondola = 0,
                            WIC = 0,
                            TransactionArea = 0,
                            GrabAndGo = 0,
                            OpenCooler = 0,
                            Freezer = 0,
                            Forecourt = 0,
                            OtherExhibition = 0,
                            ActiveCategories = 1
                        };


            decimal acumTotalSales = 0;
            foreach (var item in paretoItemsCY.Where(x => !x.IsDeletedRangeReview).OrderByDescending(x => x.Sales))
            {
                acumTotalSales += item.Sales;
                item.AcumSales = total.AverageSales.HasValue ? acumTotalSales / total.AverageSales.Value : 0;

            }

            foreach (var item in paretoItemsCY.OrderByDescending(x => x.Sales))
            {
                //Spaces
                foreach (var space in spaces.Where(x => x.Id == item.Id && x.LinearSpaceMts > 0))
                {
                    switch (space.Location1)
                    {
                        case "Gondola":
                            total.Gondola += space.LinearSpaceMts;
                            break;
                        case "WIC":
                            total.WIC += space.LinearSpaceMts;
                            break;
                        case "Transaction Area":
                            total.TransactionArea += space.LinearSpaceMts;
                            break;
                        case "Grab And Go":
                            total.GrabAndGo += space.LinearSpaceMts;
                            break;
                        case "Open Cooler":
                            total.OpenCooler += space.LinearSpaceMts;
                            break;
                        case "Freezer":
                            total.Freezer += space.LinearSpaceMts;
                            break;
                        case "Forecourt":
                            total.Forecourt += space.LinearSpaceMts;
                            break;
                        case "Other Exhibition":
                            total.OtherExhibition += space.LinearSpaceMts;
                            break;
                        default:
                            total.OtherExhibition += space.LinearSpaceMts;
                            break;
                    }
                }
            }

            total.Sales10 = paretoItemsCY.Where(x => !x.IsDeletedRangeReview && x.AcumSales <= 0.1m).Count();
            total.Sales20 = paretoItemsCY.Where(x => !x.IsDeletedRangeReview && x.AcumSales <= 0.2m).Count();
            total.Sales30 = paretoItemsCY.Where(x => !x.IsDeletedRangeReview && x.AcumSales <= 0.3m).Count();
            total.Sales40 = paretoItemsCY.Where(x => !x.IsDeletedRangeReview && x.AcumSales <= 0.4m).Count();
            total.Sales50 = paretoItemsCY.Where(x => !x.IsDeletedRangeReview && x.AcumSales <= 0.5m).Count();
            total.Sales60 = paretoItemsCY.Where(x => !x.IsDeletedRangeReview && x.AcumSales <= 0.6m).Count();
            total.Sales70 = paretoItemsCY.Where(x => !x.IsDeletedRangeReview && x.AcumSales <= 0.7m).Count();
            total.Sales80 = paretoItemsCY.Where(x => !x.IsDeletedRangeReview && x.AcumSales <= 0.8m).Count();
            total.Sales90 = paretoItemsCY.Where(x => !x.IsDeletedRangeReview && x.AcumSales <= 0.9m).Count();
            total.Sales100 = paretoItemsCY.Where(x => !x.IsDeletedRangeReview).Count();
            total.Bottom5 = paretoItemsCY.Where(x => !x.IsDeletedRangeReview && x.AcumSales >= 0.95m).Count();

            decimal? hurdleTotalRate = total.Sales100.HasValue && total.Sales100.Value != 0 ? total.AverageSales / total.Sales100 : null;
            total.HurdleRate = paretoItemsCY.Where(x => !x.IsDeletedRangeReview && x.Sales < hurdleTotalRate).Count();

            subcategoriesHistory.AddRange(categoriesHistory);
            subcategoriesHistory.Add(total);
            return subcategoriesHistory.OrderBy(x => x.Level).ThenBy(x => x.MC).ToList();
        }
    }
}
